package com.sys.console.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sys.console.business.SysEmailConfigService;
import com.sys.console.pojo.SysEmailConfig;
import com.sys.object.common.ObjException;
import com.sys.object.common.ObjResultCode;
import com.sys.object.common.ObjResultException;
import com.sys.object.common.ObjResultJSON;

/**
 * 2014-11-06
 * 
 * @author 陈成军 模块管理 action
 */
@Controller
public class SysEmailConfigAction {

	@Resource SysEmailConfigService sysEmailConfigService;
	

	@RequestMapping(value = "/sysEmailConfig.do" , method = RequestMethod.GET)
	public String sysEmailConfig(SysEmailConfig module, Model model,HttpServletRequest request) {
		try {
			//初始化模块信息
			if(!StringUtils.hasText(module.getId()))
				module.setId("1");
			SysEmailConfig sysEmailConfig = sysEmailConfigService.selectByPrimaryKey(module.getId(),request);
			model.addAttribute("entity", sysEmailConfig);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/system/emailconfig/add";
	}
	

	
	/**
	 * 新增模块 POST
	 */
	@RequestMapping(value = "/saveSysEmailConfigJson.do", method = RequestMethod.POST)
	@ResponseBody
	public ObjResultJSON<SysEmailConfig> saveSysEmailConfigJson(
			@Validated @ModelAttribute("entity") SysEmailConfig entity, Model model,HttpServletRequest request) {
		ObjResultJSON<SysEmailConfig> ret = new ObjResultJSON<SysEmailConfig>();
		try {
			int success =  sysEmailConfigService.saveOrUpdate(entity,request); //修改模块信息
			if (success > 0) {
				ret.setResultCode(ObjResultCode.OK);
				ret.setData(entity);
			} else {
				ret.setResultCode(ObjResultCode.MODULE_ADD_ERROR);
			}
		} catch (Exception e) {

			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.ERROR);
			}
		}
		return ret;
	}
	
	/**
	 * 新增模块 POST
	 */
	@RequestMapping(value = "/saveSysSmsConfigJson.do", method = RequestMethod.POST)
	@ResponseBody
	public ObjResultJSON<SysEmailConfig> saveSysSmsConfigJson(
			@Validated @ModelAttribute("entity") SysEmailConfig entity, Model model,HttpServletRequest request) {
		ObjResultJSON<SysEmailConfig> ret = new ObjResultJSON<SysEmailConfig>();
		try {
			int success =  sysEmailConfigService.saveOrUpdate(entity,request); //修改模块信息
			if (success > 0) {
				ret.setResultCode(ObjResultCode.OK);
				ret.setData(entity);
			} else {
				ret.setResultCode(ObjResultCode.MODULE_ADD_ERROR);
			}
		} catch (Exception e) {

			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.ERROR);
			}
		}
		return ret;
	}
	
	/**
	 * 删除
	 * 
	 * @param record
	 * @return
	 */
	@RequestMapping("/deleteSysEmailConfig.do")
	@ResponseBody
	public ObjResultException<String> moduleAjaxDelete(Integer id) {
		ObjResultException<String> ret = new ObjResultException<String>();
		try {
//			int success = sysModuleService.deleteByPrimaryKey(id);
//
//			if (success > 0) {
//				
//				sysModuleAuthorityMapper.deleteByMid(id); //删除关联
//				
//				ret.setResultCode(ObjResultCode.OK);
//			} else {
//				ret.setResultCode(ObjResultCode.MODULE_DELETE_ERROR);
//			}
		} catch (Exception e) {
			if (e.getClass().equals(ObjException.class)) {
				ret.setResultCode(((ObjException) e).getCode());
			} else {
				ret.setResultCode(ObjResultCode.ERROR);
			}
		}
		return ret;
	}
	
}
